package com.xuecheng.content.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author 12441
 * @Date 2022/9/15 14:29
 * @Version 1.0
 */
@Component
public class MinioTemplate {
    @Autowired
    private MinioConfig minioConfig;

    private MinioClient minioClient;
    // 声明MIME类型常量
    public static final String TEXT_HTML = "text/html";
    public static final String IMAGE_JPG = "image/jpg";

    //初始化
    @PostConstruct
    public void initMinio(){
        minioClient=MinioClient.builder()
                .credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
                .endpoint(minioConfig.getEndpoint()).build();
    }
    //图片文件上传
    public String upload(MultipartFile file){
        try {
            String fileName = file.getOriginalFilename();
            System.out.println(">>>>>>>>>>>>>>>>fileName:"+fileName);
            InputStream inputStream = file.getInputStream();

            //images/2022/09/1571fc1bdf-b9c9-4eae-8383-4b336075a5da.
//            String path="images/"+ DateTime.now().toString("yyyy/MM/dd")+ UUID.randomUUID().toString()
//                    +"."+ FileUtil.extName(fileName);
            String path="images/"+ new SimpleDateFormat("yyyy/MM/dd/").format(new Date())+
                    UUID.randomUUID().toString()
                    +"."+ FileUtil.extName(fileName);
            //构造上传的参数对象PutObjectArgs
            PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName())
                    .contentType(IMAGE_JPG)
                    .object(path).stream(inputStream, inputStream.available(), -1)
                    .build();
            //上传
            minioClient.putObject(objectArgs);

            //获取文件的访问路径
            String imageUrl = minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/" + path;
            return imageUrl;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //网页静态页面文件上传
    public String uploadHTML(InputStream inputStream,String fileName ){
        try {

            //例：pages/51.html
            String path="pages/"+fileName;
            //构造上传的参数对象PutObjectArgs
            PutObjectArgs args = PutObjectArgs.builder().bucket(minioConfig.getBucketName())
                    .contentType(TEXT_HTML)
                    .object(path).stream(inputStream, inputStream.available(), -1)
                    .build();
            //上传
            minioClient.putObject(args);

            //获取文件的访问路径
            String pageUrl = minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/" + path;
            return pageUrl;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void delete(String fileName) {
        System.out.println(">>>>>>>>>>>>>>"+fileName);
        try {

            minioClient.removeObject(minioConfig.getBucketName(), fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
